using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using DataLite;
using DataLite.Values;
using DataLite.Data;

namespace DataLite
{
    public class LiteSchema
    {
        const string TABLE = "table";
        const string VIEW = "view";

        const string SQLITE_SECUENCE = "sqlite_sequence";
        const string SQLITE_STAT = "sqlite_stat";

        const string CS_BASIC = "Data Source={0};Version=3;";
        const string CS_PASSWORD = "Data Source={0};Version=3;Password={1};";
        const string CS_POOL = "Data Source={0};Version=3;Pooling=False;Max Pool Size=100;";
        const string CS_READ_ONLY = "Data Source={0};Version=3;Read Only=True;";

        string mConnectionString;
        public string ConnectionString { get { return mConnectionString; } }

        public LiteSchema(string file)
        {
            mConnectionString = string.Format(CS_BASIC, file);

            DataHelper dataHelper = new DataHelper(mConnectionString);
            LoadSchema(dataHelper);
            dataHelper.Dispose();
        }

        #region Fields

        List<SqliteMasterVO> mMasterTable;
        /// <summary>
        /// All tables, included system tables and views
        /// </summary>
        public List<SqliteMasterVO> MasterTable { get { return mMasterTable; } }

        List<SqliteMasterVO> mUserTables;
        /// <summary>
        /// Tables created by user
        /// </summary>
        public List<SqliteMasterVO> UserTables { get { return mUserTables; } }

        List<SqliteMasterVO> mSistemTables;
        /// <summary>
        /// Tables created by system
        /// </summary>
        public List<SqliteMasterVO> SistemTables { get { return mSistemTables; } }

        List<SqliteMasterVO> mViews;
        /// <summary>
        /// All views
        /// </summary>
        public List<SqliteMasterVO> Views { get { return mViews; } }

        #endregion

        #region helpers

        public void LoadSchema(DataHelper dataHelper)
        {
            mMasterTable = SqliteMasterDAO.getInstance().SelectAll(dataHelper);
            mUserTables = new List<SqliteMasterVO>();
            mSistemTables = new List<SqliteMasterVO>();
            mViews = new List<SqliteMasterVO>();

            foreach (SqliteMasterVO sqliteMasterVO in mMasterTable)
            {
                //Informacion de las columnas
                sqliteMasterVO.ColumnInfo = ColumnInfoDAO.getInstance().PragmaTableInfo(sqliteMasterVO.Name, dataHelper);

                //Tablas del sistema
                if (sqliteMasterVO.Name == SQLITE_SECUENCE || sqliteMasterVO.Name.Contains(SQLITE_STAT))
                {
                    mSistemTables.Add(sqliteMasterVO);
                }
                else
                {
                    //Tablas de usuario
                    if (sqliteMasterVO.Type == TABLE)
                    {
                        mUserTables.Add(sqliteMasterVO);
                    }
                    else if (sqliteMasterVO.Type == VIEW)
                    {
                        mViews.Add(sqliteMasterVO);
                    }
                }
            }
        }

        #endregion
    }
}
